using System.Web;
using Microsoft.Practices.ServiceLocation;

namespace OTE.Web.Infrastructure.Session
{
    public class WebSessionState : IPersistentWebState
    {
        private readonly HttpContextBase _httpContext;

        public WebSessionState()
            : this(ServiceLocator.Current.GetInstance<HttpContextBase>())
        {
        }

        private WebSessionState(HttpContextBase httpContext)
        {
            _httpContext = httpContext;
        }

        private HttpContextBase HttpContext
        {
            get
            {
                return _httpContext;
            }
        }

        private HttpSessionStateBase Session
        {
            get
            {
                if (HttpContext != null)
                {
                    return HttpContext.Session;
                }
                return null;
            }
        }

        public bool Add(string key, object value)
        {
            Session.Add(key, value);
            return true;
        }

        public object this[string name]
        {
            get
            {
                return Session[name];
            }
            set
            {
                Session[name] = value;
            }
        }

        public bool Contains(string key)
        {
            return Session[key] != null;
        }

        public void Remove(string key)
        {
            if (Contains(key))
                Session.Remove(key);
        }
    }
}